package summitsoft.android.hiwifi.application;

import summitsoft.android.hiwifi.R;
import summitsoft.android.hiwifi.base.GeoUtils;
import summitsoft.android.hiwifi.base.ResolutionAdapter;
import summitsoft.android.hiwifi.base.WifiConst;
import android.content.Context;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class RadarLayout extends RelativeLayout{

	private Context mContext = null;
	private RadarView mRadarView = null;
	private Button mRefreshBtn = null;
	private Button mWifiListBtn = null;
	private Button mDisconnectBtn = null;
	
	private SensorManager mSensorManager;
	private LocationManager mLocationManager;
	
	public RadarLayout(Context context) {
		super(context);
		mContext = context;
		
		//radar view
		mRadarView = new RadarView(mContext);
		mRefreshBtn = new Button(mContext);
		mWifiListBtn = new Button(mContext);
		mDisconnectBtn = new Button(mContext);
		
		mRadarView.setId(WifiConst.VIEW_RADAR_ID);
		mRefreshBtn.setId(WifiConst.VIEW_BTN_REFRESH_ID);
		mWifiListBtn.setId(WifiConst.VIEW_BTN_LIST_ID);
		mDisconnectBtn.setId(WifiConst.VIEW_BTN_DISCONNECT_ID);
		
		mRefreshBtn.setText(R.string.btn_refresh);
		mWifiListBtn.setText(R.string.btn_list);
		mDisconnectBtn.setText(R.string.btn_disconnect);
		
		//init radar view
		mRadarView.setUseMetric(true);
		mRadarView.setDistanceView(mRefreshBtn);
		
		int latE6 = (int) (30.289 * GeoUtils.MILLION);
		int lonE6 = (int) (120.116 * GeoUtils.MILLION);
		mRadarView.setTarget(latE6, lonE6);
		
		mRefreshBtn.setOnClickListener(new OnClickListener(){
			//refresh wifi hot spot list
			public void onClick(View v) {
			}
        }); 
		
		mWifiListBtn.setOnClickListener(new OnClickListener(){
			//pop up wifi hot spot list
			public void onClick(View v) {
				
			}
        }); 
		
		mDisconnectBtn.setOnClickListener(new OnClickListener(){
			//disconnect all wifi spots
			public void onClick(View v) {
				
			}
        }); 
		
		addView(mRadarView);
		addView(mRefreshBtn);
		addView(mWifiListBtn);
		addView(mDisconnectBtn);
		
		setViewLayout();
		
	    mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
		mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		
		mSensorManager.registerListener(mRadarView, SensorManager.SENSOR_ORIENTATION,
	                SensorManager.SENSOR_DELAY_GAME);
	 
	        // Start animating the radar screen
		mRadarView.startSweep();

		// Register for location updates
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				WifiConst.LOCATION_UPDATE_INTERVAL_MILLIS, 0, mRadarView);
		mLocationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER,
				WifiConst.LOCATION_UPDATE_INTERVAL_MILLIS, 0, mRadarView);
	}
	
	private void setViewLayout(){
		
		if(ResolutionAdapter.isScreenLandScape()){
			
		}else{
			int nWidth = WifiConst.RADAR_WIDTH;
			int nHeight = WifiConst.RADAR_HEIGHT;
			//portrait mode
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					nWidth, nHeight);

			params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			mRadarView.setLayoutParams(params);
			
			int nRefreshBtnWidth = ResolutionAdapter.getDisplayWidth() - 2*WifiConst.BUTTON_WIDTH;
			nRefreshBtnWidth = nRefreshBtnWidth - 2*WifiConst.BUTTON_MARGIN;
			
			params = new RelativeLayout.LayoutParams(
					nRefreshBtnWidth, WifiConst.BUTTON_HEIGHT);
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			
			mRefreshBtn.setLayoutParams(params);
			
			params = new RelativeLayout.LayoutParams(
					WifiConst.BUTTON_WIDTH, WifiConst.BUTTON_HEIGHT);
			
			params.addRule(RelativeLayout.RIGHT_OF,mRefreshBtn.getId());
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			mWifiListBtn.setLayoutParams(params);
			
			params = new RelativeLayout.LayoutParams(
					WifiConst.BUTTON_WIDTH, WifiConst.BUTTON_HEIGHT);
			
			params.addRule(RelativeLayout.RIGHT_OF,mWifiListBtn.getId());
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			mDisconnectBtn.setLayoutParams(params);
			
		}
	}
	
	public void setTarget(int latE6, int lonE6){
		mRadarView.setTarget(latE6, lonE6);
	}
}